package com.gitee.sparta.core.cluster.strategy;

import com.gitee.sparta.core.bean.cluster.ClusterProtocol;
import com.gitee.sparta.core.cluster.balance.LoadBalance;
import com.gitee.sparta.core.property.SpartaCoreProperties;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;

import java.nio.channels.SocketChannel;

/**
 * <p>
 *      follower 发生了错误，不能接收消息
 * </p>
 *
 * @author zyred
 * @since v 0.0.2
 */
@Slf4j
public class FollowerErrorStrategy extends Strategy {

    public FollowerErrorStrategy(RedissonClient client,
                                LoadBalance loadBalance,
                                SpartaCoreProperties properties) {
        super(client, loadBalance, properties);
    }

    @Override
    public void handle(ChannelHandlerContext ctx, SocketChannel sc, ClusterProtocol cp) {
        log.warn("[Sparta] Follower error, sid: {}", cp.getServerId());
        this.loadBalance.removeNode(cp.getServerId());
    }
}
